<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.filesystem.file');
class upravaObjednavky extends JTable
{
	/**
	 * Primary Key
	 * @var int
	 */
	var $id 				= null;
	/** @var string */
	var $title 				= '';
	/** @var string */
	var $fulltext 				= '';
	/** @var string */
	var $jedcislo 				= '';
/** @var date */
	var $publish_up 	= 0;
/** @var date */
	var $publish_down 	= 0;
	/** @var int */
	var $catid 	= 3;
	/** @var string */
	var $created_by_alias 	= '';
	/** @var string */
	var $vlastni1 	= '';
	/** @var string */
	var $vlastni2 	= '';
	/** @var string */
	var $file 				= '';
	// zverejnovanie
  /** @var string */
	var $faktura 				= '';
  /** @var string */
	var $intCislo 				= '';
  /** @var string */
	var $dodavatel 				= 0;
  /** @var string */
	var $suma 				= '';
  /** @var string */
	var $datUhrady 				= 0;
  /** @var string */
	var $datZver 				= 0;
  /** @var string */
	var $sposob 				= '';
  /** @var string */
	var $poznamka 				= '';
  /** @var string */
	var $file2 				= '';
  /** @var string */
	var $mena 				= '';
  /** @var string */
	var $typ 				= 0;
  /** @var int */
	var $sposob_uhrady 	= '';
    /** @var int */
	var $hits 	= 0;
    /** @var int */
	var $hits2 	= 0;
      /** @var int */
	var $hits_detail 	= 0;
  /** @var date */ 
  var $datum_podpisu 	= 0;


  /** @var string */
//	var $alias	 			= '';
	/** @var int */
//	var $checked_out 		= 0;
	/** @var date */
//	var $checked_out_time 	= 0;

	function upravaObjednavky(& $db) {
    
    $this->params	= &JComponentHelper::getParams('com_jzmluvy');

		parent::__construct('#__jzmluvy', 'id', $db);
	}

	// overloaded check function
	function check()
	{
/*		// Check fields
		if (empty($this->enddates)) {
			$this->enddates = NULL;
		}

		if (empty($this->times)) {
			$this->times = NULL;
		}

		if (empty($this->endtimes)) {
			$this->endtimes = NULL;
		}

		$this->title = strip_tags(trim($this->title));
		$titlelength = JString::strlen($this->title);

		if ( $this->title == '' ) {
			$this->_error = JText::_( 'ADD TITLE' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}

		if ( $titlelength > 100 ) {
      		$this->_error = JText::_( 'ERROR TITLE LONG' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}*/
		
    
    //*** vytahuje info z config.xml
    $platnost = JRequest::getVar( 'platnost', '0', '', 'int' );

    $this->title = htmlspecialchars($this->title);
		$alias = JFilterOutput::stringURLSafe($this->title);

		if(empty($this->alias) || $this->alias === $alias ) {
			$this->alias = $alias;
		}
    
    
    //*** kontrola vybrania pola partner
    if ( $this->dodavatel == 0 ) {
			$this->_error = JText::_( 'OBJ_EDIT_CHYBA_PARTNER' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}
    
    //*** kontrola vyplnenie predmetu faktury
    if ( $this->title == '' ) {
			$this->_error = JText::_( 'OBJ_EDIT_CHYBA_PREDMET_PLNENIA' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}
    
    //*** kontrola vyplnenie sumy faktury
    if ($this->suma == '' || $this->mena == '') {
			$this->_error = JText::_( 'fa_EDIT_CHYBA_SUMA' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
      } 
    
    //*** kontrola identifikacie zmluvy a externej zmluvy, aby neboli byplnene obe !
    if ((!$this->nastavenie->crz) && (!empty($this->intCislo) || !empty($this->jedcislo)) && $this->typ) {    
			$this->_error = JText::_( 'fa_EDIT_CHYBA_IDENT_ZMLU' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
    } 
    
    //*** kontrola vyplnenie osoby; podpis objednavky
    if ( $this->vlastni1 == '' && $this->created_by_alias == 0) {
			$this->_error = JText::_( 'OBJ_EDIT_CHYBA_PODPIS' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}
    
    if ($this->datUhrady == 0) {
        	$this->_error = JText::_( 'OBJ_EDIT_CHYBA_DAT_VYHOTOVENIA' );
	      	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	      	return false;
          }
         
    if ($this->datZver == 0) {
        	$this->_error = JText::_( 'fa_EDIT_CHYBA_DAT_ZVEREJNENIE' );
	      	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	      	return false;
          }
          

    //*** kontrola formatu datumu zverejnenia pred ulozenim do DB v formate Y-m-d 
    /*
    if (!preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", jzmluvyHelper::datymd($this->datZver))) {
	      	$this->_error = JText::_( 'fa_EDIT_CHYBA_DAT_ZVEREJNENIE_FORMAT');
	      	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	      	return false;
		}
    */
    


          


		/*** kontrola vyplnenie nazov zmluvy
    if ( $this->fulltext == '' ) {
			$this->_error = JText::_( 'OBJ_EDIT_CHYBA_IDENT_ZMLUVY' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}*/
    
    /* kontrola vyplnenie cisla objednavky
    if ( $this->faktura == '' ) {
			$this->_error = JText::_( 'OBJ_EDIT_CHYBA_CISLO_OBJEDNAVKY' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}*/

    //*** preformatovanie ukladaneho datumu do DB z d.m.Y na Y-m-d
    if(!empty($this->datUhrady) || $this->datUhrady != 0) { 
      $this->datUhrady = jzmluvyHelper::datymd($this->datUhrady);;
      }
    
    //*** preformatovanie ukladaneho datumu do DB z d.m.Y na Y-m-d
    if(!empty($this->datZver) || $this->datZver != 0) { 
      $this->datZver = jzmluvyHelper::datymd($this->datZver);;
      }
    //*** preformatovanie ukladaneho datumu do DB z d.m.Y na Y-m-d
    if(!empty($this->publish_up) || $this->publish_up != 0) { 
      $this->publish_up = jzmluvyHelper::datymd($this->publish_up);
      }
    
    
    if (!preg_match( '/^[\-+]?[0-9]*\.*\,?[0-9]+$/', $this->suma)) {
    $this->_error = JText::_( 'fa_EDIT_CHYBA_SUMA_FORMAT' );
	    	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	     	return false;
         }
         else
         {
    $this->suma = jzmluvyHelper::format_number($this->suma, '.', '');     
         }

    //*** kontrola povolenej pripony pre obrazkove/scanovane subory */
		if ( $this->file['name'] != '' ) {
      $priponaSouboru = strtolower(JFile::getExt($this->file));
			$povoleneExt = explode(',',$this->params->get( 'povolene_pripony_scan', ''));		
        if (!in_array($priponaSouboru, $povoleneExt)) {
          $this->_error = JText::_( 'fa_EDIT_CHYBA_TYP').':&nbsp;'.implode($povoleneExt,', ');
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
             }
      }
          
      //*** kontrola povolenej pripony pre textove subory */
      if ( $this->file2['name'] != '' ) {
      $priponaSouboru = strtolower(JFile::getExt($this->file2));
			$povoleneExt = explode(',',$this->params->get( 'povolene_pripony_text', ''));		
        if (!in_array($priponaSouboru, $povoleneExt)) {
          $this->_error = JText::_( 'fa_EDIT_CHYBA_TYP').':&nbsp;'.implode($povoleneExt,', ');
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
          
        }
      }
    
    /*
    if (!preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/", $this->publish_up)) {
	      	$this->_error = JText::_( 'Datum vystavenia je v zlom formate, pouzi formatovanie: yyyy-mm-dd' );
	      	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	      	return false;
		} 
    
        
    if (!preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/", $this->publish_down)) {
	      	$this->_error = JText::_( 'Datum splatnosti je v zlom formate, pouzi formatovanie: yyyy-mm-dd' );
	      	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	      	return false;
		}
    
    if (!preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/", $this->datUhrady)) {
	      	$this->_error = JText::_( 'Datum uhrady je v zlom formate, pouzi formatovanie: yyyy-mm-dd' );
	      	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	      	return false;
		}
    */
    
    /*
		if (!preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/", $this->publish_down) && $platnost != 1) {
				  $this->_error = JText::_( 'Datum sejmutí je ve špatném formátu');
				  JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
				  return false;
		}  
    if ( $this->publish_up >= $this->publish_down ) {
			$this->_error = JText::_( 'Datum sejmutí musí být vyšší než vystavení' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}
    */
    
    
    /*
		if ( $this->publish_up >= $this->publish_down && $platnost != 1 ) {
			$this->_error = JText::_( 'Datum sejmutí musí být vyšší než vystavení' );
      		JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      		return false;
		}
		
		if ( $platnost == 1 ) {
      $this->publish_down = '0000-00-00';  
		}
    */
      
      
/*		if (isset($this->recurrence_counter)) {
			if (!preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/", $this->recurrence_counter)) {
	 		     	$this->_error = JText::_( 'DATE WRONG' );
	 		     	JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	 		     	return false;
			}
		}

		if (isset($this->times)) {
   			if (!preg_match("/^[0-2][0-9]:[0-5][0-9]$/", $this->times)) {
      			$this->_error = JText::_( 'TIME WRONG' );
      			JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      			return false;
	  		}
		}

		if (isset($this->endtimes)) {
   			if (!preg_match("/^[0-2][0-9]:[0-5][0-9]$/", $this->endtimes)) {
      			$this->_error = JText::_( 'TIME WRONG' );
      			JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
      			return false;
	  		}
		}

		//No venue or category choosen?
/*		if($this->locid == '') {
			$this->_error = JText::_( 'VENUE EMPTY');
			JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
			return false;
		} */

/*		if($this->catsid == 0) {
			$this->_error = JText::_( 'CATEGORY EMPTY');
			JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
			return false;
		}*/

		return true;
	}
}

?>